package es.ats.codington.festival.daos;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementSetter;


import es.ats.codington.festival.Mapper.EventMapper;
import es.ats.codington.festival.Mapper.PlaceMapper;
import es.ats.codington.festival.Mapper.SignUpMapper;
import es.ats.codington.festival.Mapper.VisitorMapper;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.Place;
import es.ats.codington.festival.exceptions.DAOSExceptions;

import es.ats.codington.festival.interfaces.daos.iEventDAO;
import es.ats.codington.festival.utils.FERSDataConnection.FersDC;
import es.ats.codington.festival.utils.Query.JSonQuery;


/**
 * The Class EventDAOImpl.
 */
public class EventDAOImpl implements iEventDAO {


	/** The jdbc template object. */
	private JdbcTemplate jdbcTemplateObject;
	
	/** The query properties. */
	private Properties queryProperties;

	
	/**
	 * Instantiates a new event dao impl.
	 *
	 * @param jdbcTemplateObject the jdbc template object
	 * @param queryProperties the query properties
	 */
	public EventDAOImpl(JdbcTemplate jdbcTemplateObject,Properties queryProperties ) {
	    this.jdbcTemplateObject = jdbcTemplateObject;
	    this.queryProperties = queryProperties;
	} 


	/**
	 * Instantiates a new event dao impl.
	 */
	public EventDAOImpl() {
		super();
	}

	

	/* (non-Javadoc)
	 * @see es.ats.codington.festival.interfaces.daos.iEventDAO#viewEvent(int)
	 */
	@Override
	public Event viewEvent(final int iDevent) throws Exception{
		
		List<Event> events = jdbcTemplateObject.query(queryProperties.getProperty("getidevent"),new PreparedStatementSetter(){
			
			@Override
			public void setValues(PreparedStatement statement)throws SQLException{
				statement.setInt(1,iDevent);
			}
		}, new EventMapper());
					
		Event event = new Event();
		event  = events.get(0);
		final int idit;
		idit = event.getPlace().getiDplace();
		
		List<Place> places = jdbcTemplateObject.query(queryProperties.getProperty("getidplace"),new PreparedStatementSetter(){
			
			@Override
			public void setValues(PreparedStatement statement)throws SQLException{
				statement.setInt(1, idit);
			}
		}, new PlaceMapper());		

		Place place = new Place();
		place = places.get(0);
		event.setPlace(place);
		
		return event;
	}

	
	
	/* (non-Javadoc)
	 * @see es.ats.codington.festival.interfaces.daos.iEventDAO#viewMyEvents(int)
	 */
	@Override
	public ArrayList<Integer> viewMyEvents(final int idvisitor) throws Exception {
		
		ArrayList<Integer> idMyEvents= new ArrayList<Integer>();
		
		List<Integer[]> events = jdbcTemplateObject.query(queryProperties.getProperty("idevent_idvisitor"),new PreparedStatementSetter(){
			
			@Override
			public void setValues(PreparedStatement statement)throws SQLException{
				statement.setInt(1, idvisitor);
				}
			}, new SignUpMapper());

		int size = events.size();
		int iter=0;
		for (iter=0; iter<size; iter++){
			idMyEvents.add(events.get(iter)[1]);
		}
		
		return idMyEvents;
	
	}
	
	/* (non-Javadoc)
	 * @see es.ats.codington.festival.interfaces.daos.iEventDAO#getIdsEvent(int)
	 */
	@Override
	public ArrayList<Integer> getIdsEvent(final int idplace) throws Exception {

		ArrayList<Integer> idsEventByType= new ArrayList<Integer>();
					
			List<Event> events = jdbcTemplateObject.query(queryProperties.getProperty("idevent_idplace"),new PreparedStatementSetter(){
				
				@Override
				public void setValues(PreparedStatement statement)throws SQLException{
					statement.setInt(1, idplace);
					}
				}, new EventMapper());
			
			int size = events.size();
			int iter=0;
			for (iter=0; iter<size; iter++){
				idsEventByType.add(events.get(iter).getiDevent());
			}
		
		return idsEventByType;
	}

	/* (non-Javadoc)
	 * @see es.ats.codington.festival.interfaces.daos.iEventDAO#createEvent(es.ats.codington.festival.domain.Event)
	 */
	@Override
	public boolean createEvent(final Event event1) throws Exception{

		int affectedRows=0;
		
		affectedRows =  jdbcTemplateObject.update(queryProperties.getProperty("create_event"),new PreparedStatementSetter(){
		
			@Override
			public void setValues(PreparedStatement statement)throws SQLException{
				
				statement.setString(1, event1.getNamevent());
				statement.setFloat(2, event1.getPrice());
				statement.setString(3, event1.getDescription());
				statement.setInt(4, event1.getPlace().getiDplace());
				statement.setInt(5, event1.getSeats());
			}
		});

		if(affectedRows!=0){
			return true;
		}
		else{
			return false;
		}
		
	}
	
}


		

	



		